// 实现 Storage，使得该对象为单例，并对 localStorage 进行封装设置值 setItem(key,value)和 getItem(key)
class Storage {
    instance = null;  //  单例模式
    static getInstance() {
        if (!instance) {
            instance = new Storage();
        }
        return instance;
    }
    setItem = (key, value) => localStorage.setItem(key, value);
    getItem = key => localStorage.getItem(key)
}

class EventEmitter {
    constructor() {
        this.events = {}
    }
    on(event, callback) {
        this.events[event].push(callback)
    }
    emit(event, ...args) {
        this.events[event].forEach(fn => fn(...args))
    }
    off(event, callback) {
        this.events[event].filter(fn => fn !== callback)  // 注意
    }
    once(event, callback) {
        function wrapFn(...args) {
            this.off(event, wrapFn)
            callback.call(this, ...args)
        }
        this.on(evnet, wrapFn)
    }
}